<?php

namespace api\modules\v1\forms\clinic;

use Yii;
use yii\base\Model;
use common\models\member\Member;
use common\models\base\Profession;
use common\models\clinic\Clinic;

/**
 *
 * Class RelationKefuForm
 * @package api\modules\v1\forms\clinic
 */
class RelationKefuForm extends Model
{
    /**
     * 客服ID
     *
     * @var
     */
    public $kefu_id;

    /**
     * 项目ID
     *
     * @var
     */
    public $project_id;

    /**
     * 诊室ID
     *
     * @var
     */
    public $clinic_ids;

    /**
     * 绑定诊室列表
     *
     * @var
     */
    protected $relationKefuList;

    /**
     * 删除诊室IDs
     *
     * @var array
     */
    protected $deleteClinicIds = [];

    /**
     * 新增诊室IDs
     *
     * @var array
     */
    protected $addClinicIds = [];


    public function rules()
    {
        return [
            // 参数验证
            [['project_id', 'kefu_id'/*, 'clinic_ids'*/], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],

            [['project_id'], 'checkProjectId', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['kefu_id'], 'checkKefuId', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['clinic_ids'], 'checkClinicIds', 'skipOnEmpty' => false, 'on' => ['edit']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 编辑客服绑定诊室
        $scenarios['edit'] = ['project_id', 'kefu_id', 'clinic_ids'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'project_id' => '项目ID',
            'kefu_id' => '客服ID',
            'clinic_ids' => '诊室',
        ];
    }

    /**
     * 检验项目ID
     *
     * @param $attribute
     * @return bool
     */
    public function checkProjectId($attribute)
    {
        if (Yii::$app->user->identity->member->is_super == Member::IS_SUPER_YES) {
            return true;
        }

        $attr = Yii::$app->services->attr->findInfo([
            'member_id' => Yii::$app->user->identity->member_id,
            'project_id' => $this->project_id,
        ]);

        if (!$attr) {
            $this->addError($attribute, '未绑定当前项目，无法操作');
            return true;
        }
    }

    /**
     * 检验客服ID
     *
     * @param $attribute
     * @return bool
     */
    public function checkKefuId($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        // 判断是否为客服
        $member = Yii::$app->services->attr->findInfo([
            'member_id' => $this->kefu_id,
            'project_id' => $this->project_id,
        ]);
        if (empty($member)) {
            $this->addError($attribute, '参数错误[kefu_id_error]');
            return true;
        }

        if (!in_array($member->profession_id, [Profession::ID_CUSTOMER_LEADER, Profession::ID_CUSTOMER])) {
            $this->addError($attribute, '非客服人员，无法操作');
            return true;
        }
    }

    /**
     * 校验诊室IDs
     *
     * @param $attribute
     */
    public function checkClinicIds($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        /*if (empty($this->clinic_ids)) {
            $this->addError($attribute, "诊室不能为空");
            return true;
        }*/

        // 诊室列表
        $clinicIdArr = $this->clinic_ids ? array_unique(explode(',', $this->clinic_ids)) : [];
        $clinicList = Yii::$app->services->clinic->findList([
            'id' => $clinicIdArr
        ]);
        foreach ($clinicList as $row) {
            if ($row['status'] != Clinic::STATUS_ENABLE) {
                $this->addError($attribute, "参数错误[clinic_id_error]");
                return true;
            }
        }
        $hasClinicIds = array_column($clinicList, 'id');

        // 已绑定诊室列表
        $kefuList = $this->getRelationKefuList();
        $clinicIds = array_column($kefuList, 'clinic_id');

        // 删除诊室IDs
        $this->deleteClinicIds = array_diff($clinicIds, $hasClinicIds);

        // 交集，存在部分
        $hasIds = array_intersect($clinicIds, $hasClinicIds);

        // 新增诊室IDs
        $this->addClinicIds = array_diff($hasClinicIds, $hasIds);
    }

    /**
     * 获取绑定诊室列表
     *
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getRelationKefuList()
    {
        if (empty($this->relationKefuList)) {
            $this->relationKefuList = Yii::$app->services->clinicRelationKefu->findList([
                'kefu_id' => $this->kefu_id,
                'project_id' => $this->project_id,
            ]);
        }

        return $this->relationKefuList;
    }

    /**
     * 获取删除诊室IDs
     *
     * @return array
     */
    public function getDeleteClinicIds()
    {
        return $this->deleteClinicIds;
    }

    /**
     * 获取新增诊室IDs
     *
     * @return array
     */
    public function getAddClinicIds()
    {
        return $this->addClinicIds;
    }
}
